using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class UserHeadCtrlBase : UserControl
	{
		private const string TAG = "UserHeadCtrlBase";
		public static DependencyProperty ItemObjectProperty = DependencyProperty.Register("ItemObject", typeof(string), typeof(UserHeadCtrlBase), new PropertyMetadata(null, new PropertyChangedCallback(UserHeadCtrlBase.OnItemObjectPropertyChanged)));
		public static DependencyProperty mObjectReqProperty = DependencyProperty.Register("mObjectReq", typeof(object), typeof(UserHeadCtrlBase), new PropertyMetadata(null, new PropertyChangedCallback(UserHeadCtrlBase.OnmObjectReqPropertyChanged)));
		public static DependencyProperty mCtrlArgProperty = DependencyProperty.Register("mCtrlArg", typeof(CtHeadArg), typeof(UserHeadCtrlBase), new PropertyMetadata(null, new PropertyChangedCallback(UserHeadCtrlBase.OnmCtrlArgPropertyChanged)));
		private bool mBIsLoaded;
		private static EventWatcher mWatchHeadImg;
		private static object mObjectWatcher = new object();
		private static Dictionary<string, List<UserHeadCtrlBase>> sDictWaitCtrls = new Dictionary<string, List<UserHeadCtrlBase>>();
		private object mObject;
		private string mStrUserRequesting;
		public event MMUIEvent mDlgHeadImgGetted;
		public string mStrUsername
		{
			get
			{
				return (string)base.GetValue(UserHeadCtrlBase.ItemObjectProperty);
			}
			set
			{
				base.SetValue(UserHeadCtrlBase.ItemObjectProperty, value);
			}
		}
		public object mObjectReq
		{
			get
			{
				return base.GetValue(UserHeadCtrlBase.mObjectReqProperty);
			}
			set
			{
				base.SetValue(UserHeadCtrlBase.mObjectReqProperty, value);
			}
		}
		public CtHeadArg mCtrlArg
		{
			get
			{
				return (CtHeadArg)base.GetValue(UserHeadCtrlBase.mCtrlArgProperty);
			}
			set
			{
				base.SetValue(UserHeadCtrlBase.mCtrlArgProperty, value);
			}
		}
		public UserHeadCtrlBase()
		{
			base.add_Loaded(new RoutedEventHandler(this.UserHeadCtrlBase_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.UserHeadCtrlBase_Unloaded));
			ShowObjectInMemory.AddOneObjectRecord(this);
		}
		~UserHeadCtrlBase()
		{
			ShowObjectInMemory.RemoveOneObjectRecord(this);
		}
		private static void OnItemObjectPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == UserHeadCtrlBase.ItemObjectProperty)
			{
				(dependencyObject_0 as UserHeadCtrlBase).ItemObjectPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void ItemObjectPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				if (dependencyPropertyChangedEventArgs_0.get_NewValue() == null)
				{
					return;
				}
				this.StartRequest();
			}
		}
		private static void OnmObjectReqPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == UserHeadCtrlBase.mObjectReqProperty)
			{
				(dependencyObject_0 as UserHeadCtrlBase).mObjectReqPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void mObjectReqPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				this.mCtrlArg = CtHeadArg.GetArg(this.mObjectReq);
			}
		}
		private static void OnmCtrlArgPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == UserHeadCtrlBase.mCtrlArgProperty)
			{
				(dependencyObject_0 as UserHeadCtrlBase).mCtrlArgPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void mCtrlArgPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				this.StartRequest();
			}
		}
		private void UserHeadCtrlBase_Unloaded(object sender, RoutedEventArgs e)
		{
			this.mBIsLoaded = false;
		}
		private void UserHeadCtrlBase_Loaded(object sender, RoutedEventArgs e)
		{
			this.mBIsLoaded = true;
			this.objectGetted(this.mObject);
			this.mObject = null;
		}
		private static void registerEvent()
		{
			if (UserHeadCtrlBase.mWatchHeadImg == null)
			{
				UserHeadCtrlBase.mWatchHeadImg = new EventWatcher(UserHeadCtrlBase.mObjectWatcher, null, new EventHandlerDelegate(UserHeadCtrlBase.OnHeadImg));
				EventCenter.registerEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, UserHeadCtrlBase.mWatchHeadImg);
			}
		}
		private static void unRegisterEvent()
		{
			if (UserHeadCtrlBase.mWatchHeadImg != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, UserHeadCtrlBase.mWatchHeadImg);
				UserHeadCtrlBase.mWatchHeadImg = null;
			}
		}
		protected virtual void ObjectCome(string strUser)
		{
			if (this.mDlgHeadImgGetted != null)
			{
				this.mDlgHeadImgGetted(this, strUser);
			}
		}
		private void objectGetted(object object_0)
		{
			if (object_0 == null)
			{
				return;
			}
			this.StopRequest();
			if (!this.mBIsLoaded)
			{
				this.mObject = object_0;
				return;
			}
			this.ObjectCome(object_0 as string);
		}
		private static void getOneSnsImg(string strUserName)
		{
			if (strUserName == null)
			{
				return;
			}
			if (UserHeadCtrlBase.sDictWaitCtrls.ContainsKey(strUserName))
			{
				List<UserHeadCtrlBase> list = UserHeadCtrlBase.sDictWaitCtrls.get_Item(strUserName);
				using (List<UserHeadCtrlBase>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						UserHeadCtrlBase current = enumerator.get_Current();
						if (current != null)
						{
							current.objectGetted(strUserName);
						}
					}
				}
			}
		}
		private static void addOneCtrlToWait(string strUser, UserHeadCtrlBase ctrl)
		{
			List<UserHeadCtrlBase> list;
			if (!UserHeadCtrlBase.sDictWaitCtrls.ContainsKey(strUser))
			{
				list = new List<UserHeadCtrlBase>();
				UserHeadCtrlBase.sDictWaitCtrls.Add(strUser, list);
			}
			else
			{
				list = UserHeadCtrlBase.sDictWaitCtrls.get_Item(strUser);
			}
			if (!list.Contains(ctrl))
			{
				list.Add(ctrl);
			}
		}
		private static void remOneCtrlFromWait(string strUser, UserHeadCtrlBase ctrl)
		{
			if (!UserHeadCtrlBase.sDictWaitCtrls.ContainsKey(strUser))
			{
				return;
			}
			List<UserHeadCtrlBase> list = UserHeadCtrlBase.sDictWaitCtrls.get_Item(strUser);
			if (!list.Contains(ctrl))
			{
				list.Remove(ctrl);
			}
			if (list.get_Count() == 0)
			{
				UserHeadCtrlBase.sDictWaitCtrls.Remove(strUser);
			}
		}
		private static void OnHeadImg(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			EventConst mEventID = evtArgs.mEventID;
			if (mEventID != EventConst.ON_MGR_HEAD_IMG_READY)
			{
				return;
			}
			if (evtArgs.mObject == null)
			{
				return;
			}
			if (evtArgs.mObject is List<object>)
			{
				List<object> listObject = evtArgs.getListObject<object>();
				if (listObject == null)
				{
					return;
				}
				using (List<object>.Enumerator enumerator = listObject.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						string strUserName = (string)enumerator.get_Current();
						UserHeadCtrlBase.getOneSnsImg(strUserName);
					}
					return;
				}
			}
			UserHeadCtrlBase.getOneSnsImg(evtArgs.mObject as string);
		}
		protected void StartRequest(string strUser)
		{
			CtHeadArg arg = CtHeadArg.GetArg(strUser);
			this.mCtrlArg = arg;
		}
		private void StartRequest()
		{
			if (!CtHeadArg.IsArgComplete(this.mCtrlArg))
			{
				this.StopRequest();
				return;
			}
			if (this.mStrUserRequesting == this.mCtrlArg.mStrUserName)
			{
				return;
			}
			this.mStrUserRequesting = this.mCtrlArg.mStrUserName;
			if (UserHeadCtrlBase.mWatchHeadImg == null)
			{
				UserHeadCtrlBase.registerEvent();
			}
			Contact contact = StorageMgr.contact.method_0(this.mStrUserRequesting);
			BitmapImage headImage;
			if (contact != null)
			{
				headImage = HeadImgMgr.getHeadImage(contact);
			}
			else
			{
				headImage = HeadImgMgr.getHeadImage(this.mCtrlArg.mObjForRequest);
			}
			if (headImage != HeadImgMgr.defaultHeadImage)
			{
				this.ObjectCome(this.mStrUserRequesting);
				this.StopRequest();
				return;
			}
			UserHeadCtrlBase.addOneCtrlToWait(this.mCtrlArg.mStrUserName, this);
		}
		public void StopRequest()
		{
			if (this.mStrUserRequesting != null)
			{
				if (this.mStrUserRequesting != null)
				{
					UserHeadCtrlBase.remOneCtrlFromWait(this.mStrUserRequesting, this);
				}
				this.mStrUserRequesting = null;
			}
		}
	}
}
